#include "node_scene.h"


NodeScene::NodeScene(int index,QWidget *parent):QWidget(parent)
{
    //item = new NodeItem("long time node");
    item = new NodeItemNumber();
    SetIndex(index);
    initSocket(1,1);
}

NodeScene::NodeScene(int index, int type, QWidget *parent):QWidget(parent)
{
    if(type == 0){
        item = new NodeItemOutput();
        SetIndex(index);
        initSocket(1,0);
    }
    else if(type == 1){
        item = new NodeItemNumber();
        SetIndex(index);
        initSocket(1,1);
    }
    else if(type == 2){
        item = new NodeItemAdd();
        SetIndex(index);
        initSocket(2,1);
    }
    else if(type == 3){
        item = new NodeItemSub();
        SetIndex(index);
        initSocket(2,1);
    }
    else if(type == 4){
        item = new NodeItemMul();
        SetIndex(index);
        initSocket(2,1);
    }
    else if(type == 5){
        item = new NodeItemDiv();
        SetIndex(index);
        initSocket(2,1);
    }
    else if(type == 9){
        item = new NodeGraphicsShow();
        SetIndex(index);
        initSocket(1,1);
    }
    //set input ouput

}

NodeScene::NodeScene(const NodeScene &nodeScene,QWidget *parent):QWidget(parent)
{
    Q_UNUSED(nodeScene);
    this->index = nodeScene.index;

}

void NodeScene::operator=(const NodeScene &nodeScene)
{
    this->index = nodeScene.index;
    Q_UNUSED(nodeScene);
}

NodeScene::~NodeScene()
{
    qDebug()<<"delete node"<<this->index;
    delete item;
    item = NULL;
    input.clear();
    output.clear();
}

void NodeScene::initSocket(int number_left,int number_right)
{
    input_number =number_left;
    output_number = number_right;
    for(int i=0;i<input_number; i++)
    {
        NodeSocket *socket = new NodeSocket(item);
        socket->setPos(0,i*20+34);
        socket->SetIndex(i);
        socket->SetType(SOCKET_INPUT);
        input.append(socket);
    }
    for(int i=0;i<output_number; i++){

        NodeSocket *socket2 = new NodeSocket(item);
        socket2->setPos(180,i*20+34);
        socket2->SetIndex(i);
        socket2->SetType(SOCKET_OUTPUT);
        output.append(socket2);

    }

}

void NodeScene::SetIndex(int n)
{
    this->index = n;
}

void NodeScene::SetItemTitle(QString title)
{
    this->title = title;
    this->item->SetTitle(title);
}

int NodeScene::GetSelectedSocetIndex()
{
    //return this
    for(int i=0;i<input.length();i++){
        if(input[i]->selected_index){
            return input[i]->index;
        }
    }
    return -1;
}

void NodeScene::DebugOutput()
{
    for(int z=0;z<input.length();z++){
        qDebug()<<"socket is"<<this->index<<" "<<input[z]->pos();
    }

    for(int z=0;z<output.length();z++){
        qDebug()<<"socket is"<<this->index<<" "<<output[z]->pos();
    }
}

////////////////////

NodeSceneOutput::NodeSceneOutput(int index,QWidget *parent):QWidget(parent)
{
    //item = new NodeItem("long time node");
    item = new NodeItemOutput();
    SetIndex(index);
    initSocket(3,1);
}

NodeSceneOutput::NodeSceneOutput(const NodeSceneOutput &nodeScene,QWidget *parent):QWidget(parent)
{
    Q_UNUSED(nodeScene);
    this->index = nodeScene.index;

}

void NodeSceneOutput::operator=(const NodeSceneOutput &nodeScene)
{
    this->index = nodeScene.index;
    Q_UNUSED(nodeScene);
}

NodeSceneOutput::~NodeSceneOutput()
{
    qDebug()<<"delete node";
    delete item;
    item = NULL;
    input.clear();
    output.clear();
}

void NodeSceneOutput::initSocket(int number_left,int number_right)
{
    input_number =number_left;
    output_number = number_right;
    for(int i=0;i<input_number; i++)
    {
        NodeSocket *socket = new NodeSocket(item);
        socket->setPos(0,i*20+34);
        socket->SetIndex(i);
        socket->SetType(SOCKET_INPUT);
        input.append(socket);
    }
    for(int i=0;i<output_number; i++){

        NodeSocket *socket2 = new NodeSocket(item);
        socket2->setPos(180,i*20+34);
        socket2->SetIndex(i);
        socket2->SetType(SOCKET_OUTPUT);
        output.append(socket2);

    }

}

void NodeSceneOutput::SetIndex(int n)
{
    this->index = n;
}

void NodeSceneOutput::SetItemTitle(QString title)
{
    this->title = title;
    this->item->SetTitle(title);
}

int NodeSceneOutput::GetSelectedSocetIndex()
{
    //return this
    for(int i=0;i<input.length();i++){
        if(input[i]->selected_index){
            return input[i]->index;
        }
    }
    return -1;
}
